import '../flutter_base_framework.dart';

/// http 配置
abstract class IHttpConfig{

  // 错误code
  abstract var errorCode;

  // 请求成功code
  abstract var successCode;

  // token 失效code, 需要登录
  abstract var tokenInvalidCode;


  NotSuccessException handlerError(e, s) {
    printErrorStack(e, s);
    if (e is DioError) {
      switch (e.type) {
        case DioErrorType.cancel:
          return NotSuccessException(code: errorCode, message: "请求取消");
        case DioErrorType.connectTimeout:
          return NotSuccessException(code: errorCode, message: "连接超时");
        case DioErrorType.receiveTimeout:
          return NotSuccessException(code: errorCode, message: "响应取消");
        case DioErrorType.sendTimeout:
          return NotSuccessException(code: errorCode, message: "请求超时");
        case DioErrorType.response:
          return NotSuccessException(code: errorCode, message: "服务器异常");
        default:
          var error = e.error;
          if (error is NotSuccessException) {
            if (error.code == tokenInvalidCode) {
              // 判断错误code 执行重新登录操作
              tokenInvalidHandler();
            }
            return error;
          } else {
            return NotSuccessException(code: errorCode, message: "未知错误");
          }
      }
    } else {
      return NotSuccessException(code: errorCode, message: "未知错误");
    }
  }

  void tokenInvalidHandler();
}